<?php
/* ========================================
 * --------------------
 *  DataBase.php
 * --------------------
 * Created on 29.03.2010
 * ========================================
 * $Revision: $
 * $Author: Dmitry Boycov
 * $HeadURL: $ 
 * $Id: $
 * ========================================
 * Changes ["date: description (who)"]:
 *
 * 29.03.2010: Original version (WHo)
 * 16:28:16: Time 
 */
require_once("include/bd/Result.php");

class DataBase{

	private $host = null;

	private $user = null;

	private $pwd = null;

	private $DBName = null;

	private $stageConnection = 'DB is not coonected';

	private $connection = null;
	private $query_time = 0;

	/**
	 * construct
	 * @param  $host
	 * @param  $user
	 * @param  $pwd
	 * @param  $DBName
	 * @return void
	 */
	public function __construct($host=null,$user = null,$pwd = null,$DBName =null){
		$this->host = $host;
		$this->user = $user;
		$this->pwd = $pwd;
		$this->DBName = $DBName;
	}

	public function getStageConnection(){
        return $this->stageConnection;
    }

    public function setDBName($DBname){
		$this->DBName = $DBname;
	}

	public function getDBName(){
		return $this->DBName;
	}

	public function setHost($host){
		$this->host = $host;
	}

	public function setUser($user){
		$this->user = $user;
	}

	public function setPwd($pwd){
		$this->user = $pwd;
	}

	public function getHost(){
		return $this->host;
	}

	public function getUser(){
		return $this->user;
	}

	public function getPwd(){
		return $this->pwd;
	}

	/**
	 * @return boolean connected or not connected
	 */
	public function connect(){
		if(!is_null($this->host) || !is_null($this->user) || !is_null($this->pwd) || !is_null($this->DBName)){
        //if(is_null($this->host) || is_null($this->user) || is_null($this->DBName)){
			$this->connection = mysql_connect($this->host, $this->user, $this->pwd);
			if($this->connection){
				$this->stageConnection = 'BD is connected';
				if(mysql_select_db($this->DBName)){
					$this->stageConnection = 'BD is selected';
					return true;
				}else{
					$this->stageConnection = 'BD is not selected';
					die($this->stageConnection.'.'.mysql_error());
					return false;
				}
			}else{
				$this->stageConnection = 'BD is not connected';
				die($this->stageConnection . mysql_error());
				return false;
			}
		}else{
			$this->stageConnection = 'enough parameters for connect';
			die($this->stageConnection.'.'.mysql_error());
			return false;
		}
	}

	private function checkConnection(){
		if(!isset($this->connection)) {
			$this->connectDB();
		}
		return $this->connection;
	}

	/**
	 * @param  $query - Query object
	 * @return boolean
	 */
	public function query($query){
		// TO DO 1.execute query 2. fill result object 3. free_result
		$res = null;
		if ($query!=null){
			$sql = $query->execute();
			if(is_null($sql) || $sql==''){
				return null;
			}
			$this->checkConnection();
			$this->query_time = microtime(1);
			//$sql = mb_convert_encoding($sql,$config['dbCharset'],$config['clientCharset']);
			$result = mysql_query($sql);
			$this->query_time =$this->query_time-microtime(1);
			$res = new Result($result);
//            mysql_free_result($result);/////!!!!!!!!!!
		}
		return $res;
	}

	public function close(){
		if(isset($this->connection)){
			//$this->freeResult();
			mysql_close($this->connection);
			unset($this->connection);
		}
	}

}

?>